package com.lankystar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 
 * @author success
 * 
 *         2015-10-10--4:40:29
 */
public class DateUtil {

	/**
	 * @param year
	 */
	public static boolean isLeapYear(int year) {
		/*
		 * if((year%4==0 && year%100!=0) || year%400==0) return true; return
		 * false;
		 */
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	}

	/**
	 */
	public static int getMaxDays(int year, int month) {
		int maxDay = -1;
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			maxDay = 31;
		} else if (month == 2) {
			maxDay = isLeapYear(year) ? 29 : 28;
		} else {
			maxDay = 30;
		}
		return maxDay;
	}

	/**
	 */
	public static int getMaxDay(int year, int month) {
		int day = -1;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = isLeapYear(year) ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}
	/**
	 */
	public static Date createDate(int year,int month,int date){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month-1, date);
		return cal.getTime();
	}
	/**
	 */
	public static int getDayOfWeek(int year,int month){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month-1, 1);
		return cal.get(Calendar.DAY_OF_WEEK)-1;
	}
	/**
	 */
	public static String format(Date d,String pattern){
		String sdate = null;
		if(d==null || "".equals(d) || "".equals(pattern) || pattern==null){
		}else{
			sdate = new SimpleDateFormat(pattern).format(d);
		}
		return sdate;
	}
	
	/**
	 */
	public static Date parse(String sdate,String pattern){
		Date d = null;
		if(null==sdate || "".equals(sdate) || pattern==null || "".equals(pattern)){
			System.err.println("!!!");
		}else{
			try {
				d = new SimpleDateFormat(pattern).parse(sdate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return d;
	}
	
	/**
	 */
	public static String getStringDayOfWeek(Date d){
		return new SimpleDateFormat("EEE").format(d);
	}
}
